package com.ffyc.resale.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.model.Forum;
import com.ffyc.resale.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.util.List;

// 帖子dao层
public interface ForumDao extends BaseMapper<Forum> {


    @Select("SELECT f.*, u.name AS userName, u.avatar AS userImg " +
            "FROM forum f LEFT JOIN user u ON f.userid = u.id " +
            "${ew.customSqlSegment}")
    Page<Forum> selectForumPageWithUser(Page<Forum> page, @Param(Constants.WRAPPER) QueryWrapper<Forum> ew);

    @Select("SELECT u.id,u.name,u.age,u.gender,u.email,u.avatar " +
            "FROM user u LEFT JOIN forum f ON  u.id = f.userid " +
            "WHERE f.id = #{forumId}")
    User userInfo(String forumId);

    @Insert("insert into forum(title,content,forum_img,create_time,userid,digest)values(#{title},#{content},#{forumImg},now(),#{userid},#{digest})")
    void addForum(Forum forum);

    @Select("SELECT f.*, u.name as userName, u.avatar as userImg " +
            "FROM forum f LEFT JOIN user u ON f.userid = u.id " +
            "WHERE f.id = #{forumId}")
    Forum findForumInfoById(@Param("forumId") Integer forumId);

    @Select("SELECT \n" +
            "    f.id,\n" +
            "    f.title,\n" +
            "    u.avatar userImg,\n" +
            "    u.name userName,\n" +
            "    f.digest,\n" +
            "    f.forum_img \n" +
            "FROM \n" +
            "    forum f \n" +
            "LEFT JOIN \n" +
            "    user u ON f.userid = u.id \n" +
            "ORDER BY \n" +
            "    f.readed DESC \n" +
            "LIMIT 5;")
    List<Forum> topForums(Forum forum);
}
